<?php

namespace App\Providers;

use DB;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        DB::listen(function ($query){

//            $sql_arr=explode('?',$query->sql);
//            $sql='';
//            foreach ($sql_arr as $k=>$v){
//                $sql .= $sql_arr[$k].$v;
//            }
//            $sql .=$sql_arr[count($sql_arr)-1];
//            die($sql);

            //打开一个文本文档,写入日志
            $fp=fopen(storage_path('logs/dblog.txt'),'a');
            $arr=[
                $query->sql,
                $query->bindings,
                $query->time,
                $_SERVER['REMOTE_ADDR'],
                data('Y-m-d H:m:s')
            ];
            fwrite($fp,json_encode($arr)."\n");
            fclose($fp);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
